/*
 * twi_func.c
 *
 * Created: 15.09.2013 22:27:31
 *  Author: maxim
 */ 

#include <stdio.h>
#include "../GccLibrary1/TWI_Master.h"

void twi_setup() {
    TWI_Master_Initialise();
}

#define PCA_ADDR 0b1110000

void twi_test() {
	unsigned char twi_buf[TWI_BUFFER_SIZE];
	unsigned char i;
	
	printf("Test TWI\r\n");
	twi_buf[0]=PCA_ADDR<<1;
	twi_buf[1]=0b10000000;
	for(i=2;i<TWI_BUFFER_SIZE;i++) {
		twi_buf[i]=0;
	}
	TWI_Start_Transceiver_With_Data(twi_buf,2);

	twi_buf[0]=(PCA_ADDR<<1)|1;
	TWI_Start_Transceiver_With_Data(twi_buf,0x1B+1);
	
	while(TWI_Transceiver_Busy()) {
	}
	
	if(TWI_statusReg.lastTransOK) {
		TWI_Get_Data_From_Transceiver(twi_buf,0x1B+1);
		printf("\r\nresult from PCA:\r\n");
		for(i=0;i<TWI_BUFFER_SIZE;i++) {
			printf("Register:%x - %x\r\n",i-1,twi_buf[i]);
		}
		} else {
		printf("\r\n Error from PCA:%x",TWI_Get_State_Info());
	}
	
}


